class obj_base{
  constructor(json){
    if (typeof (json) === 'object') {
      this.init(json);
    }
  }
  init(json){
    //  to do nothing...
    //  唯一标识
    this.id = json.id;
  }
}

//  地址model对象
class obj_address extends obj_base{
  init(json){
    super.init(json);
    //  用户名
    this.name = json.name;
    //  手机号
    this.phone = json.phone;
    //  省份
    this.province = json.province;
    //  城市
    this.city = json.city;
    //  地址
    this.address = json.address;
    //  距离
    this.distance = json.distance;
    //  是否为默认
    this.default = json.default;
  }
}

//  优惠券model对象
class obj_coupon extends obj_base {
  init(json) {
    super.init(json);
    //  标题
    this.name = json.name;
    //  图片
    this.image = json.image;
    //  是否已经使用了
    this.used = json.used;

    //  优惠券信息
    var coupontype = json.coupontype;
    this.goodlist = coupontype.goodlist;
    this.name = coupontype.name;
    this.money = coupontype.money;
    this.begindate = coupontype.begindate;
    this.enddate = coupontype.enddate;
    this.expiryDate = coupontype.begindate.split(" ")[0];
    //  优惠金额
    this.discount = coupontype.discount;
    //  优惠券类型[2:现金抵扣,1:折扣]
    this.couponType = coupontype.type;
    //  文字介绍
    this.desc = coupontype.desc;
  }
}

//  分页model对象
class obj_page extends obj_base {
  init(json){
    this.current_page = json.current_page;
    this.first_page_url = json.first_page_url;
    this.last_page = json.last_page;
    this.last_page_url = json.last_page_url;
    this.next_page_url = json.next_page_url;
    this.path = json.path;
    this.per_page = json.per_page;
    this.prev_page_url = json.prev_page_url;

    let tmpItems = json.data;
    if (tmpItems != null) {
      var goodsItems = [];
      tmpItems.forEach((tmpItem) => {
        var tmpGoodsItem = new obj_goods(tmpItem);
        goodsItems.push(tmpGoodsItem);
      });
      this.goodsItems = goodsItems;
    }
  }
}

//  商品model对象
class obj_goods extends obj_base {
  init(json){
    super.init(json);
    //  商品名字
    this.name = json.name;
    //  商品价格
    this.price = json.baseprice;
    //  商品描述
    this.brief = json.brief;
    //  商品图片
    let tmpImages = json.images;
    var imagePaths = []
    tmpImages.forEach((item)=>{
      var imagePath = json.imageurl + item
      imagePaths.push(imagePath)
    })

    this.images = imagePaths;
    //  商品是否为新品
    this.is_new = json.isnew;
    //  商品是否可定制
    this.is_customize = json.is_customize;

    //  商品的类型组
    var optionItems = []
    json.tasteall.forEach((optionItem,index)=>{

      if(optionItem != null) {
        let tmpOptionItem = new obj_goodsOptionItem(optionItem);
        tmpOptionItem.checked = index == 0 ? true : false
        optionItems.push(tmpOptionItem);
      }
    })
    this.optionItems = optionItems

    //  商品的尺寸组
    var sizeItems = []
    json.sizeall.forEach((sizeItem,index)=>{
      if(sizeItem != null) {
        let tmpSizeItem = new obj_goodsSizeItem(sizeItem);
        tmpSizeItem.isSelected = index == 0 ? true : false
        sizeItems.push(tmpSizeItem);
      }
    })
    
    this.sizeItems = sizeItems;

    //  商品价格区间
    this.pricehighlow = json.pricehighlow;
    let pricehigh = json.pricehigh;
    let pricelow = json.pricelow;
    if (pricelow == pricehigh){
      this.pricehighlow = pricehigh.toFixed(2)
    }
    else{
      this.pricehighlow = pricelow.toFixed(2) + "-" + pricehigh.toFixed(2)
    }

    //  商品是否定期售卖[周一、周二、...、周日]
    let fixdays = json.fixday;
    if(fixdays instanceof Array){
      //  处理后端返回一个空字符串
      let removeIndex = fixdays.indexOf("")
      if(removeIndex > -1) {
        fixdays.splice(removeIndex, 1)
      }

      if(fixdays.length > 0) {
        this.fixBuy = true;
        this.fixdays = fixdays;
        this.sellshow = json.sellshow;

        //  数组中有值表示是限购，则需要判断今天是否可以购买
        let date = new Date();
        let weekDay = date.getDay().toString;
        let result = fixdays.indexOf(result)
        this.canBuy = result > -1 ? true : false
      }
    }

    //  库存值
    // let invertory = json.invertory;
    this.invertory = json.invertory

    //  创建时间
    this.created_at = json.created_at;
  }
}

//  商品分类model对象
class obj_category extends obj_base {
  init(json) {
    super.init(json);
    //  分类名字
    this.name = json.name;
    //  分类类型
    this.type = json.type;
    //  商品价格
    this.selected = json.selected || false;

    this.goodItems = []
  }
}

//  商品优惠model对象
class obj_discountItem extends obj_base {
  init(json){
    super.init(json);
    //  限时时间
    this.time = json.time;
    //  背景图片
    this.icon = json.icon;
    //  原价
    this.original_price = json.original_price;
    //  现价
    this.current_price = json.current_price;
    //  标题
    this.title = json.title;
  }
}

//  商品类型
class obj_goodsOptionItem extends obj_base{
  init(json) {
    super.init(json);
    //  
    this.material = json.desc;
    //  蛋糕胚
    this.title = json.title;
    //  价格

    this.price = json.price;
    //  价格
    this.sweet = json.sweet;
    //  食材成分组
    var tmpIngredientsItems = json.ingredientshow.split(";");

    //  移除数组中为“”的对象
    tmpIngredientsItems = tmpIngredientsItems.filter(tmpItem=>{
      return tmpItem.length > 0
    })

    var tmpSectionItems = [];
    for (var i = 0; i < tmpIngredientsItems.length; i += 2) {
      tmpSectionItems.push(tmpIngredientsItems.slice(i, i + 2));
    }
    this.ingredientsSectionItems = tmpSectionItems;
  }
}

//  商品尺寸
class obj_goodsSizeItem extends obj_base{
  init(json){
    super.init(json);
    this.name = json.name;
    this.price = json.price == null ? 0.00 : parseFloat(json.price);
    
    const descIds = json.desc
    const descshowItems = json.descshow.split(";")
    
    descshowItems.forEach((tmpItem) => {
      if (tmpItem.length > 0) {      
        var value = null
        var key = null
        if(tmpItem.indexOf(":") != -1){
          var key = tmpItem.split(":")[0]
          var value = tmpItem.split(":")[1]
        }
        if (tmpItem.indexOf("：") != -1) {
          key = tmpItem.split("：")[0]
          value = tmpItem.split("：")[1]
        }

        // this.book = "需提前3日预定"
        // this.storage = "0-5摄氏度冷藏保存"


        if (value != null && key != null) {
          if(key === "尺寸"){
              this.size = value
          }
          else if (key === "餐具") {
            this.ware = value
          }
          else if (key === "适合人数") {
            this.person = value
          }
          else if (key ==="提前预定"){
            this.book = value
          }
          else if(key ==="存储条件"){
            this.storage = value
          }
        }
      }
    })
  }
}

//  购物车中的商品
class obj_cartGoodsItem extends obj_base{
  constructor(goodsItem,optionItem,sizeItem,count){
    super()
    this.goodsItem = goodsItem;
    this.optionItem = optionItem;
    this.sizeItem = sizeItem;
    this.count = count;

    this.price = goodsItem.price + (optionItem == null ? 0 : optionItem.price) + (sizeItem == null ? 0 : sizeItem.price);
    this.priceText = this.price.toFixed(2)

    this.amountText = "0.0"
    this.amount = 0.0
    this.is_selected = false
  }

  showAmountText(){
    this.amount = this.price * this.count
    this.amountText = this.amount.toFixed(2)
  }
}

//  订单
class obj_orderItem extends obj_base{
  init(json){
    super.init(json);
    //  收货人信息
    var tmpAddressInfo = json.address;
    if (tmpAddressInfo == null && json.addressid == -1){
      //  门店自取
      this.deliverType = 0;
    }
    else {
      this.deliverType = 1;
      this.address = new obj_address(tmpAddressInfo);
    }
    // //  商品组
    var goodsTotalCount = 0;
    var orderlis = json.orderlis;
    var orderGoodsItems = []
    orderlis.forEach((tmpItem)=>{
      var tmpGoodItem = new obj_goods(tmpItem.good);
      var tmpTasteid = tmpItem.tasteid;
      var tmpSizeid = tmpItem.sizeid;

      var tmpOptionItem = null;
      var tmpSizeItem = null;

      //  1.0.4版本
      var tmpTasteJson = tmpItem.taste;
      var tmpSizeJson = tmpItem.size;
      if(tmpTasteJson != null){
        tmpOptionItem = new obj_goodsOptionItem(tmpTasteJson);
      }

      if (tmpSizeJson != null) {
        tmpSizeItem = new obj_goodsSizeItem(tmpSizeJson);
      }

      let count = tmpItem.quantity;
      goodsTotalCount += count;

      if(tmpOptionItem == null) {
        tmpGoodItem.optionItems.forEach((optionItem) => {
          if (optionItem.id == tmpTasteid) {
            tmpOptionItem = optionItem
          }
        })
      }
      
      if(tmpSizeItem == null) {
        tmpGoodItem.sizeItems.forEach((sizeItem) => {
          if (sizeItem.id == tmpSizeid) {
            tmpSizeItem = sizeItem
          }
        })
      }
      
      var tmpCartGoodItem = new obj_cartGoodsItem(tmpGoodItem, tmpOptionItem, tmpSizeItem, count)
      orderGoodsItems.push(tmpCartGoodItem);
    })

    this.goodsTotalCount = goodsTotalCount;

    this.orderGoods = orderGoodsItems;
    // //  总金额
    this.price = json.real_pay.toFixed(2);
    // //  使用的优惠券
    var tmpCouponInfo = json.coupon;
    if (tmpCouponInfo != null){
      this.coupon = new obj_coupon(tmpCouponInfo);
    }
    else{
      this.coupon = null;
    }
    
    // //  抵扣的积分
    // this.point = json.point;
    // //  订单状态 0 表示待完成 1 表示已完成
    //  0 未支付  1 支付成功 -1 取消支付  2 已发货
    this.orderType = json.status;
    //
    let bookdate = json.bookdate;
    let bookenddate = json.bookenddate;
    // //  订单日期
    this.orderDay = bookdate.split(" ")[0];
    // //  订单时间
    this.orderTime = bookdate.split(" ")[1] +"-"+ bookenddate.split(" ")[1];
  }
}


export {
  obj_address,
  obj_coupon,
  obj_goods,
  obj_goodsOptionItem,
  obj_goodsSizeItem,
  obj_category,
  obj_discountItem,
  obj_cartGoodsItem,
  obj_orderItem,
  obj_page
};